import json
import sys
import scrapy
import scrapy
from scrapy import Request, Selector

sys.path.append('../spiders')
from Taobao.items import TaobaoItem


class YMX(scrapy.Spider):
    name = 'ymx'
    allowed_domains = ["harrimanclothing.com"]

    def start_requests(self):
        for j in range(1, 2):
            url = f"https://harrimanclothing.com/collections/special-occasion-clearance-sale?page={j}"
            yield Request(url=url)

    def parse(self, response, **kwargs):
        sel = Selector(response)
        selectors = sel.xpath('//*[@id="main-collection-product-grid"]//div//div[@class="product-details"]')
        for i in selectors:
            item = TaobaoItem()
            u1 = i.xpath('./a/@href').extract_first().strip()
            urls = "https://harrimanclothing.com" + u1 + ".js"
            item["PageUrl"] = "https://harrimanclothing.com" + u1
            yield Request(url=urls, callback=self.parse_json, cb_kwargs={'item': item})

    def parse_json(self, response, **kwargs):
        jsonresp = json.loads(response.text)
        item = kwargs['item']
        if jsonresp.get("compare_at_price_max") == 0:
            item["price"] = str(jsonresp.get('price'))
            item['sprice'] = ""
            str2 = "." + item["price"][-2:]
            item["price"] = item["price"][0:len(item["price"]) - 2]
            item["price"] = item["price"] + str2
        else:
            item['sprice'] = str(jsonresp.get("price"))
            item["price"] = str(jsonresp.get('compare_at_price_max'))
            str3 = "." + item["sprice"][-2:]
            str4 = "." + item["price"][-2:]
            item["price"] = item["price"][0:len(item["price"]) - 2]
            item["sprice"] = item["sprice"][0:len(item["sprice"]) - 2]
            item["sprice"] = item["sprice"]+str3
            item["price"] = item["price"]+str4
        if item['sprice'] == item["price"]:
            item['sprice'] = '0'
        item["title"] = jsonresp.get('title')
        item["image"] = "https" + jsonresp.get('featured_image')
        item["gallery"] = ""
        try:
            images = jsonresp.get('images')
            for img in images[1:]:
                item["gallery"] += str("https" + img)
                item["gallery"] += ";"
            item["gallery"] = item["gallery"][0:len(item["gallery"]) - 1]
        except:
            print("没有二级图片")
        item['sku'] = jsonresp.get('variants')[0]['sku']
        item["color"] = ""
        item['size'] = ""
        try:
            options = jsonresp.get('options')
            for option in options:
                if option['name'] == "Color":
                    colors = option['values']
                    for col in colors:
                        item["color"] += col
                        item["color"] += ","
                    item["color"] = item["color"][0:len(item["color"]) - 1]
        except:
            print("没有颜色")
        try:
            options1 = jsonresp.get('options')
            for option1 in options1:
                if option1['name'] == "Size":
                    sizes = option1['values']
                    for size in sizes:
                        item["size"] += size
                        item["size"] += ","
                    item["size"] = item["size"][0:len(item["size"]) - 1]
        except:
            print("没有大小")
        item['description'] = jsonresp.get("description") or ""
        item['category'] = "Clearance||Special Occasion Dresses"
        yield item
